package restless.utilities.testing {
	import flash.display.*;
	import flash.utils.*;
	import flash.text.*;
 
	public class DictionarySpeedTest extends Sprite
	{
		public function DictionarySpeedTest()
		{
			const SIZE:uint = 6000;
			const REPS:uint = 2000;
			var i:int;
			var s:String;
			var a:int;
			var b:String;
			var rep:int;
			var res:String = "For-each times:\n";
			var before:uint;
			var after:uint;
			var arr:Array = new Array(SIZE);
			for (i = 0; i < SIZE; ++i)
			{
				arr[i] = int(Math.random()*1000);
			}
			var vecFixed:Vector.<int> = new Vector.<int>(SIZE, true);
			for (i = 0; i < SIZE; ++i)
			{
				vecFixed[i] = arr[i];
			}
			var vecVariable:Vector.<int> = new Vector.<int>(SIZE, false);
			for (i = 0; i < SIZE; ++i)
			{
				vecVariable[i] = arr[i];
			}
			var bmdAlpha:BitmapData = new BitmapData(SIZE, 1, true);
			for (i = 0; i < SIZE; ++i)
			{
				bmdAlpha.setPixel32(i, 0, arr[i]);
			}
			var bmdNoAlpha:BitmapData = new BitmapData(SIZE, 1, false);
			for (i = 0; i < SIZE; ++i)
			{
				bmdNoAlpha.setPixel32(i, 0, arr[i]);
			}
			var obj:Object = {};
			for (i = 0; i < SIZE; ++i)
			{
				obj[i] = i;
			}
			var dictStrong:Dictionary = new Dictionary();
			for (i = 0; i < SIZE; ++i)
			{
				dictStrong[i] = i;
			}
			var dictWeak:Dictionary = new Dictionary(true);
			for (i = 0; i < SIZE; ++i)
			{
				dictWeak[i] = i;
			}
			var ba:ByteArray = new ByteArray();
			ba.length = SIZE;
			for (i = 0; i < SIZE; ++i)
			{
				ba[i] = i;
			}
 
			before = getTimer();
			for (rep = 0; rep < REPS; ++rep)
			{
				for each (i in arr)
				{
					a = i;
				}
			}
			after = getTimer();
			res += "\tArray: " + (after-before) + "\n";
 
			before = getTimer();
			for (rep = 0; rep < REPS; ++rep)
			{
				for each (i in vecFixed)
				{
					a = i;
				}
			}
			after = getTimer();
			res += "\tFixed vec: " + (after-before) + "\n";
 
			before = getTimer();
			for (rep = 0; rep < REPS; ++rep)
			{
				for each (i in vecVariable)
				{
					a = i;
				}
			}
			after = getTimer();
			res += "\tVariable vec: " + (after-before) + "\n";
 
			before = getTimer();
			for (rep = 0; rep < REPS; ++rep)
			{
				for each (i in obj)
				{
					a = i;
				}
			}
			after = getTimer();
			res += "\tObject: " + (after-before) + "\n";
 
			before = getTimer();
			for (rep = 0; rep < REPS; ++rep)
			{
				for each (i in dictStrong)
				{
					a = i;
				}
			}
			after = getTimer();
			res += "\tDictionary (strong keys): " + (after-before) + "\n";
 
			before = getTimer();
			for (rep = 0; rep < REPS; ++rep)
			{
				for each (i in dictWeak)
				{
					a = i;
				}
			}
			after = getTimer();
			res += "\tDictionary (weak keys): " + (after-before) + "\n";
 
			res += "For-in times:\n";
 
			before = getTimer();
			for (rep = 0; rep < REPS; ++rep)
			{
				for (s in arr)
				{
					a = arr[s];
				}
			}
			after = getTimer();
			res += "\tArray: " + (after-before) + "\n";
 
			before = getTimer();
			for (rep = 0; rep < REPS; ++rep)
			{
				for (s in vecFixed)
				{
					a = vecFixed[s];
				}
			}
			after = getTimer();
			res += "\tFixed vec: " + (after-before) + "\n";
 
			before = getTimer();
			for (rep = 0; rep < REPS; ++rep)
			{
				for (s in vecVariable)
				{
					a = vecVariable[s];
				}
			}
			after = getTimer();
			res += "\tVariable vec: " + (after-before) + "\n";
 
			before = getTimer();
			for (rep = 0; rep < REPS; ++rep)
			{
				for (s in obj)
				{
					a = obj[s];
				}
			}
			after = getTimer();
			res += "\tObject: " + (after-before) + "\n";
 
			before = getTimer();
			for (rep = 0; rep < REPS; ++rep)
			{
				for (s in dictStrong)
				{
					a = dictStrong[s];
				}
			}
			after = getTimer();
			res += "\tDictionary (strong keys): " + (after-before) + "\n";
 
			before = getTimer();
			for (rep = 0; rep < REPS; ++rep)
			{
				for (s in dictWeak)
				{
					a = dictWeak[s];
				}
			}
			after = getTimer();
			res += "\tDictionary (weak keys): " + (after-before) + "\n";
 
			res += "For times:\n";
 
			before = getTimer();
			for (rep = 0; rep < REPS; ++rep)
			{
				for (i = 0; i < SIZE; ++i)
				{
					a = arr[i];
				}
			}
			after = getTimer();
			res += "\tArray: " + (after-before) + "\n";
 
			before = getTimer();
			for (rep = 0; rep < REPS; ++rep)
			{
				for (i = 0; i < SIZE; ++i)
				{
					a = vecFixed[i];
				}
			}
			after = getTimer();
			res += "\tFixed vec: " + (after-before) + "\n";
 
			before = getTimer();
			for (rep = 0; rep < REPS; ++rep)
			{
				for (i = 0; i < SIZE; ++i)
				{
					a = vecVariable[i];
				}
			}
			after = getTimer();
			res += "\tVariable vec: " + (after-before) + "\n";
 
			before = getTimer();
			for (rep = 0; rep < REPS; ++rep)
			{
				for (i = 0; i < SIZE; ++i)
				{
					a = bmdAlpha.getPixel32(i, 0);
				}
			}
			after = getTimer();
			res += "\tBMD w/ alpha getPixel32: " + (after-before) + "\n";
 
			before = getTimer();
			for (rep = 0; rep < REPS; ++rep)
			{
				for (i = 0; i < SIZE; ++i)
				{
					a = bmdNoAlpha.getPixel32(i, 0);
				}
			}
			after = getTimer();
			res += "\tBMD w/o alpha getPixel32: " + (after-before) + "\n";
 
			before = getTimer();
			for (rep = 0; rep < REPS; ++rep)
			{
				for (i = 0; i < SIZE; ++i)
				{
					a = bmdAlpha.getPixel(i, 0);
				}
			}
			after = getTimer();
			res += "\tBMD w/ alpha getPixel: " + (after-before) + "\n";
 
			before = getTimer();
			for (rep = 0; rep < REPS; ++rep)
			{
				for (i = 0; i < SIZE; ++i)
				{
					a = bmdNoAlpha.getPixel(i, 0);
				}
			}
			after = getTimer();
			res += "\tBMD w/o alpha getPixel: " + (after-before) + "\n";
 
			before = getTimer();
			for (rep = 0; rep < REPS; ++rep)
			{
				for (i = 0; i < SIZE; ++i)
				{
					a = obj[i];
				}
			}
			after = getTimer();
			res += "\tObject: " + (after-before) + "\n";
 
			before = getTimer();
			for (rep = 0; rep < REPS; ++rep)
			{
				for (i = 0; i < SIZE; ++i)
				{
					a = dictStrong[i];
				}
			}
			after = getTimer();
			res += "\tDictionary (strong keys): " + (after-before) + "\n";
 
			before = getTimer();
			for (rep = 0; rep < REPS; ++rep)
			{
				for (i = 0; i < SIZE; ++i)
				{
					a = dictWeak[i];
				}
			}
			after = getTimer();
			res += "\tDictionary (weak keys): " + (after-before) + "\n";
 
			before = getTimer();
			for (rep = 0; rep < REPS; ++rep)
			{
				for (i = 0; i < SIZE; ++i)
				{
					a = ba[i];
				}
			}
			after = getTimer();
			res += "\tByteArray: " + (after-before) + "\n";
 
			var tf:TextField = new TextField();
			tf.autoSize = TextFieldAutoSize.LEFT;
			tf.text = res;
			addChild(tf);
		}
	}
}